package com.hp.gxw.u8.xml;

import com.hp.gxw.u8.dto.CustomerParameter;
import com.hp.gxw.utils.DateUtil;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;


public class CustomerXml {
	
	public static String createXML(CustomerParameter customerParameter) throws Exception{
		try {
			// 1、创建document对象
			Document document = DocumentHelper.createDocument();
			// 2、创建根节点rss
			Element ufinterface = document.addElement("ufinterface");
			ufinterface.addNamespace("z", "http://www.w3.org/2001/XMLSchema");
			// 向ufinterface节点添加efserverid属性(U8账套)
			ufinterface.addAttribute("efserverid", customerParameter.getEfserverid());
			// 向ufinterface节点添加eftype属性(方法：24代表采购入库)
			ufinterface.addAttribute("eftype", customerParameter.getEftype());
			// 向ufinterface节点添加proc属性(增删改查事件)
			ufinterface.addAttribute("proc", customerParameter.getProc());
			// 向ufinterface节点添加efdebug属性
			ufinterface.addAttribute("efdebug", "1");
			// 向ufinterface节点添加actions属性,入库后自动审核
			ufinterface.addAttribute("actions", "verify");
			// 4、生成子节点及子节点内容
			Element voucher = ufinterface.addElement("Customer");
			voucher.addElement("code").setText(customerParameter.getCode());// 传入参数：单据类型
			voucher.addElement("name").setText(customerParameter.getName());// 传入参数
			voucher.addElement("abbrname").setText(customerParameter.getAbbrname());// 传入参数
			voucher.addElement("sort_code").setText(customerParameter.getSort_code());// 传入参数
			voucher.addElement("ccusexch_name").setText(customerParameter.getCcusexch_name());// 传入参数
			if(customerParameter.getDomain_code() != null){
				voucher.addElement("domain_code").setText(customerParameter.getDomain_code());// 传入参数：仓库
			}
			if(customerParameter.getTax_reg_code() != null){
				voucher.addElement("tax_reg_code").setText(customerParameter.getTax_reg_code());// 传入参数：到货单号
			}
			if(customerParameter.getBank_open() != null){
				voucher.addElement("bank_open").setText(customerParameter.getBank_open());// 传入参数：供应商
			}
			if(customerParameter.getLegal_man() != null){
				voucher.addElement("legal_man").setText(customerParameter.getLegal_man());// 传入参数：供应商
			}
			if(customerParameter.getCRegCash() != null){
				voucher.addElement("cRegCash").setText(customerParameter.getCRegCash());// 传入参数：供应商
			}
			if(customerParameter.getBank_acc_number() != null){
				voucher.addElement("bank_acc_number").setText(customerParameter.getBank_acc_number());// 传入参数：供应商
			}
			if(customerParameter.getAddress() != null){
				voucher.addElement("address").setText(customerParameter.getAddress());// 传入参数：供应商
			}
			if(customerParameter.getContact() != null){
				voucher.addElement("contact").setText(customerParameter.getContact());// 传入参数：供应商
			}
			if(customerParameter.getEmail() != null){
				voucher.addElement("email").setText(customerParameter.getEmail());// 传入参数：供应商
			}
			if(customerParameter.getPhone() != null){
				voucher.addElement("phone").setText(customerParameter.getPhone());// 传入参数：供应商
			}
			if(customerParameter.getMobile() != null){
				voucher.addElement("mobile").setText(customerParameter.getMobile());// 传入参数：供应商
			}
			if(customerParameter.getBCusDomestic() != null){
				voucher.addElement("bCusDomestic").setText(customerParameter.getBCusDomestic());// 传入参数：供应商
			}
			if(customerParameter.getBCusOverseas() != null){
				voucher.addElement("bCusOverseas").setText(customerParameter.getBCusOverseas());// 传入参数：供应商
			}
			if(customerParameter.getBServiceAttribute() != null){
				voucher.addElement("bServiceAttribute").setText(customerParameter.getBServiceAttribute());// 传入参数：供应商
			}
			voucher.addElement("seed_date").setText(DateUtil.getDateTimeFormat(customerParameter.getSeed_date()));// 传入参数：供应商
			voucher.addElement("dcuscreatedatetime").setText(DateUtil.getDateTimeFormat(customerParameter.getDcuscreatedatetime()));// 传入参数：供应商
			if(customerParameter.getCreatePerson() != null){
				voucher.addElement("CreatePerson").setText(customerParameter.getCreatePerson());// 传入参数：供应商
			}
			voucher.addElement("ccusmngtypecode").setText(customerParameter.getCcusmngtypecode());// 传入参数：供应商
			OutputFormat format = OutputFormat.createPrettyPrint();
			// 设置编码格式
			format.setEncoding("UTF-8");
			
			return document.asXML();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
}
